package com.ruoyi.flowable.controller;

import java.util.List;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.SysDeployForm;
import com.ruoyi.flowable.service.ISysDeployFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.flowable.domain.SysForm;
import com.ruoyi.flowable.service.ISysFormService;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 表单配置Controller
 *
 * @author heyue
 * @date 2022-04-24
 */
@RestController
@RequestMapping("/flowable/form")
@Slf4j
public class SysFormController extends BaseController
{
    @Autowired
    private ISysFormService sysFormService;

    @Autowired
    private ISysDeployFormService sysDeployFormService;
    /**
     * 查询表单配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysForm sysForm)
    {
        startPage();
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        return getDataTable(list);
    }

    /**
     * 导出表单配置列表
     */
    @Log(title = "表单配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysForm sysForm) throws IOException
    {
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        ExcelUtil<SysForm> util = new ExcelUtil<SysForm>(SysForm.class);
        util.exportExcel(response, list, "表单配置数据");
    }

    /**
     * 获取表单配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        try {
            SysForm sysForm = sysFormService.selectSysFormById(id);
            return AjaxResult.success(sysForm);
        }catch (CustomException e){
            return AjaxResult.error(HttpStatus.MOVED_PERM,e.getMessage());
        }

    }

    /**
     * 新增表单配置
     */
    @Log(title = "表单配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysForm sysForm)
    {
        return toAjax(sysFormService.insertSysForm(sysForm));
    }

    /**
     * 修改表单配置
     */
    @Log(title = "表单配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysForm sysForm)
    {
        return toAjax(sysFormService.updateSysForm(sysForm));
    }

    /**
     * 删除表单配置
     */
    @Log(title = "表单配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysFormService.deleteSysFormByIds(ids));
    }
    /**
     * 获取流程业务配置
     */
    @GetMapping(value = "/getDeployForm/{id}")
    public AjaxResult getDeployForm(@PathVariable("id") String id)
    {
        SysDeployForm sysDeployForm = sysDeployFormService.selectSysDeployFormByDeployID(id);
        if(Objects.nonNull(sysDeployForm))
            return AjaxResult.success(sysDeployForm);
        else{
            return AjaxResult.success(new SysDeployForm());
        }

    }
    /**
     * 挂载流程业务
     */
    @Log(title = "挂载流程业务", businessType = BusinessType.INSERT)
    @PostMapping("/addDeployForm")
    public AjaxResult addDeployForm(@RequestBody SysDeployForm sysDeployForm) {
        return toAjax(sysDeployFormService.insertSysDeployForm(sysDeployForm));
    }
}
